﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace ATM
{
    public partial class guitien : Form
    {
        private SqlConnection conn;
        private SqlCommand cmd,cmd1;
        private SqlDataReader rd;
        public guitien(string s)
        {
            InitializeComponent();
            label2.Text = s;
        }

        private void guitien_Load(object sender, EventArgs e)
        {
            string conStr=ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            conn = new SqlConnection(conStr);
            cmd = new SqlCommand("select Acc_Balance from TB_ACCOUNT where Acc_No='" + label2.Text + "'", conn);
            conn.Open();
            rd = cmd.ExecuteReader();
            rd.Read();
            textBox1.Text = rd.GetValue(0).ToString();
            conn.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            main f = new main(label2.Text);
            f.Show();
            this.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "")
            {
                MessageBox.Show("Chua nhap so tien");
            }
            else
            {
                cmd = new SqlCommand("update TB_ACCOUNT set Acc_Balance=Acc_Balance+'" + textBox2.Text + "' where Acc_No='" + label2.Text + "'", conn);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                string s = (float.Parse(textBox1.Text) + float.Parse(textBox2.Text)).ToString();
                cmd = new SqlCommand("select Acc_ID from TB_ACCOUNT where Acc_No='" + label2.Text + "'", conn);
                conn.Open();
                rd = cmd.ExecuteReader();
                rd.Read();
                string s1 = rd.GetValue(0).ToString();
                conn.Close();
                cmd1 = new SqlCommand("insert into TB_TRANSACTION(Tran_Type,Acc_Pri_ID,Tran_Amount,Tran_Balance,Tran_Date) values (0,'" + s1 + "','" + textBox2.Text + "','" + s + "','" + DateTime.Now.ToShortDateString() + "')", conn);
                string s2 = DateTime.Now.ToShortDateString();
                conn.Open();
                cmd1.ExecuteNonQuery();
                //  MessageBox.Show("OK");
                conn.Close();
                if (MessageBox.Show("Giao dịch thành công.Bạn có muốn lấy hóa đơn không?", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    hdguitien f = new hdguitien(label2.Text, textBox2.Text, s, s2);
                    f.Show();
                    this.Hide();

                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button4.Text;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button5.Text;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button6.Text;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button7.Text;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button8.Text;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button10.Text;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button9.Text;
        }

        private void button12_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button12.Text;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button11.Text;
        }

        private void button13_Click(object sender, EventArgs e)
        {
            textBox2.Text = textBox2.Text + button13.Text;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox2.Text = "";
        }
    }
}
